global class Assignment5Class {


   public  webService static List<String> getStudents(String classId) {
        List<Student__c> studentList = [select First_Name__c, Last_Name__c from Student__c where Class__r.id =:classId];
        List<String> studentNameList = new List<String>();
        for(Student__c record: studentList){
            studentNameList.add(record.First_Name__c + record.Last_Name__c);
        }
        return studentNameList;
    }
    
    public static Map<id, Student__c> testMap() {
        Map<id,Student__c> mapAct = new Map<id,Student__c>([select Id, Name from Student__c]);
        //Map<id,Account> mapAct = new Map<id,Account>([select id,Name from Account]) 12323123;
        Map<id, String> testMap = new Map<id, String>();
        return mapAct;
    }
    static testMethod void testGetClassStudentMap() {
        Map<id, Set<id>> retMap = getClassStudentMap();
        System.debug(logginglevel.INFO, 'Returned Map is --->' + retMap);
    }
    
    /**
        This method is not retrieving the classes with empty list of students in
        it.
    */
    public static Map<id, Set<id>> getClassStudentMap() {
        Map<id, Set<id>> retMap = new Map<id, Set<id>>();
        Map<id, Student__c> classStudentMap = new Map<id,Student__c>([select id, Class__r.id from Student__c]);
                
        Set<id> studentIdSet = classStudentMap.keyset();
        for(id studentId : studentIdSet) {
            //get the student sObject for this student
            Student__c stuClass = classStudentMap.get(studentId);
            
            //get the classId for the student 
            id studentClassId = stuClass.Class__r.id;
            
            //find if the retMap has already been populated for this classid
            Set<id> studentSetInRetMap = retMap.get(studentClassId);
            if(studentSetInRetMap == null) {
                //create a new Set with studentid in it.
                studentSetInRetMap = new Set<id>();
                retMap.put(studentClassId, studentSetInRetMap);
                //studentSetInRetMap.add(stuClass.id);                
            }
            
            studentSetInRetMap.add(stuClass.id);
        }
        //we need return type of Map<id, Set<id>> = Map<class_id, Set<list_of_student_ids>>
        return retMap;
    }
    
    static testMethod void testGetWorkingDays(){
        Date beginDate = date.parse('1/1/2010');
        Date endDate = date.parse('1/30/2010');
        System.debug(logginglevel.INFO, '********* begin *******************' );
        Integer workingDays = getWorkingDays(beginDate, endDate) ;
        System.debug(logginglevel.INFO, '---- workingDays ---> ' + workingDays  );
        System.debug(logginglevel.INFO, '********* end *******************' );
    }
    
    
    /**
        No date validations.
    */
    public static Integer getWorkingDays(Date beginDate, Date endDate) {
    
        Integer daysDiff = beginDate.daysBetween(endDate);
        System.debug(logginglevel.INFO, '--- daysDiff ======= ' + daysDiff);
        Date startWeekForBeginDate = beginDate.toStartOfWeek();        
        System.debug(logginglevel.INFO, '--- startWeekForBeginDate ======= ' + startWeekForBeginDate );
        
        Boolean isStartDayAHoliday = (startWeekForBeginDate.daysBetween(beginDate) == 0 || startWeekForBeginDate.daysBetween(beginDate) == 6 );
                
        Date startWeekForEndDate = endDate.toStartOfWeek();
        System.debug(logginglevel.INFO, '--- startWeekForEndDate ======= ' + startWeekForEndDate );
        
        Boolean isEndDayAHoliday = (startWeekForEndDate.daysBetween(endDate) == 0 || startWeekForEndDate.daysBetween(endDate) == 6 );
        
        Integer startWeekDaysDiff = startWeekForBeginDate.daysBetween(startWeekForEndDate);
        System.debug(logginglevel.INFO, '--- startWeekDaysDiff ======= ' + startWeekDaysDiff);
        Integer noOfWeeksBetStartWeekDates = startWeekDaysDiff/7 ;
        System.debug(logginglevel.INFO, '--- noOfWeeksBetStartWeekDates ======= ' + noOfWeeksBetStartWeekDates);
        Integer noOfHolidays = noOfWeeksBetStartWeekDates * 2;
        System.debug(logginglevel.INFO, '--- noOfHolidays ======= ' + noOfHolidays );
        
        //beginDate and endDate may be a holiday. check for that
        //TODO: include the day for beginDate if not sunday or saturday
        //TODO: if endDate is saturday then do not include it
        // week starts on sunday
        if(isStartDayAHoliday ){
            if(isEndDayAHoliday) {
                return daysDiff - noOfHolidays -1;
            }else 
                return daysDiff - noOfHolidays;
        }else {
            if(isEndDayAHoliday) {
                return daysDiff - noOfHolidays;
            } else{
                return daysDiff - noOfHolidays + 1  ;
            }
        }
    }
    

}